﻿#ifndef __LCD_HAL_H
#define __LCD_HAL_H

#include "bsp.h"
#include "common.h"

/*****************************************************************************
* @brief   dir 
*****************************************************************************/
#define USE_HORIZONTAL 0 //定义是否使用横屏 		0,不使用.1,使用.


/*****************************************************************************
* @brief   size 
*****************************************************************************/
#if USE_HORIZONTAL == 1 //使用横屏
#define LCD_W 320
#define LCD_H 240
#else
#define LCD_W 240
#define LCD_H 320
#endif

/*****************************************************************************
* @brief 宏
*****************************************************************************/
#define stc_inline __STATIC_INLINE
#define lcd_hal_sig_keep() NOP2()

/*****************************************************************************
* @brief IO 操作
*****************************************************************************/
#define lcd_hal_rst_high() \
	GPIOD->BSRR = BIT(13); \
	lcd_hal_sig_keep() //复位端口
#define lcd_hal_cs_high()  \
	GPIOD->BSRR = BIT(10); \
	lcd_hal_sig_keep() //片选端口
#define lcd_hal_rs_high() \
	GPIOB->BSRR = BIT(2); \
	lcd_hal_sig_keep() //数据/命令
#define lcd_hal_wr_high() \
	GPIOB->BSRR = BIT(1); \
	lcd_hal_sig_keep() //写数据
#define lcd_hal_rd_high() \
	GPIOB->BSRR = BIT(0); \
	lcd_hal_sig_keep() //读数据

#define lcd_hal_rst_low()        \
	GPIOD->BSRR = BIT(13) << 16; \
	lcd_hal_sig_keep() //复位端口
#define lcd_hal_cs_low()         \
	GPIOD->BSRR = BIT(10) << 16; \
	lcd_hal_sig_keep() //片选端口
#define lcd_hal_rs_low()        \
	GPIOB->BSRR = BIT(2) << 16; \
	lcd_hal_sig_keep() //数据/命令
#define lcd_hal_wr_low()        \
	GPIOB->BSRR = BIT(1) << 16; \
	lcd_hal_sig_keep() //写数据
#define lcd_hal_rd_low()        \
	GPIOB->BSRR = BIT(0) << 16; \
	lcd_hal_sig_keep() //读数据

/*****************************************************************************
* @brief io r/w
*****************************************************************************/
stc_inline void lcd_hal_io16_out(u16 dat)
{
	register uu32 msk;

	msk = BITS(0, 5) | BITS(13, 15);
	GPIOC->BSRR = (msk << 16) | (dat & msk);

	msk = BITS(6, 7);
	GPIOA->BSRR = (msk << 16) | (dat & msk);

	msk = BITS(8, 12); //=> PE11-PE15
	GPIOE->BSRR = ((msk << 16) | (dat & msk)) << (11 - 8);
}

stc_inline void lcd_hal_io8_out(u16 dat)
{
	register uu32 msk;

	msk = BITS(0, 5);
	GPIOC->BSRR = (msk << 16) | (dat & msk);

	msk = BITS(6, 7);
	GPIOA->BSRR = (msk << 16) | (dat & msk);
}

stc_inline u16 lcd_hal_io16_in(void)
{
	register uu32 msk, dat;

	msk = BITS(0, 5) | BITS(13, 15);
	dat = GPIOC->IDR & msk;

	msk = BITS(6, 7);
	dat |= GPIOA->IDR & msk;

	msk = BITS(8, 12);
	dat |= (GPIOE->IDR >> (11 - 8)) & msk;

	return (uu16)dat;
}

/*****************************************************************************
* @brief write data
*****************************************************************************/
#define lcd_hal_dat_write(data) \
	lcd_hal_rs_high();          \
	lcd_hal_io16_out(data);     \
	lcd_hal_wr_low();           \
	lcd_hal_wr_high();

/*****************************************************************************
* @brief reg select
*****************************************************************************/
#define lcd_hal_reg_sel(reg) \
	lcd_hal_rs_low();        \
	lcd_hal_io8_out(reg);    \
	lcd_hal_wr_low();        \
	lcd_hal_wr_high();

#define lcd_hal_reg_set(reg, dat) \
	lcd_hal_rs_low();             \
	lcd_hal_io8_out(reg);         \
	lcd_hal_wr_low();             \
	lcd_hal_wr_high();            \
	lcd_hal_rs_high();            \
	lcd_hal_io8_out(dat);         \
	lcd_hal_wr_low();             \
	lcd_hal_wr_high();

/*****************************************************************************
* @brief  写GRAM
*****************************************************************************/
#define lcd_write_gram_begin() \
	lcd_hal_cs_low();          \
	lcd_hal_reg_sel(R34);      \
	lcd_hal_rs_high();

#define lcd_write_gram(data) \
	lcd_hal_io16_out(~data);  \
	lcd_hal_wr_low();        \
	lcd_hal_wr_high();

#define lcd_write_gram_end() \
	lcd_hal_cs_high();

/*****************************************************************************
* @brief  9320/9325 LCD reg
*****************************************************************************/
#define R0 0x00
#define R1 0x01
#define R2 0x02
#define R3 0x03
#define R4 0x04
#define R5 0x05
#define R6 0x06
#define R7 0x07
#define R8 0x08
#define R9 0x09
#define R10 0x0A
#define R12 0x0C
#define R13 0x0D
#define R14 0x0E
#define R15 0x0F
#define R16 0x10
#define R17 0x11
#define R18 0x12
#define R19 0x13
#define R20 0x14
#define R21 0x15
#define R22 0x16
#define R23 0x17
#define R24 0x18
#define R25 0x19
#define R26 0x1A
#define R27 0x1B
#define R28 0x1C
#define R29 0x1D
#define R30 0x1E
#define R31 0x1F
#define R32 0x20
#define R33 0x21
#define R34 0x22
#define R36 0x24
#define R37 0x25
#define R40 0x28
#define R41 0x29
#define R43 0x2B
#define R45 0x2D
#define R48 0x30
#define R49 0x31
#define R50 0x32
#define R51 0x33
#define R52 0x34
#define R53 0x35
#define R54 0x36
#define R55 0x37
#define R56 0x38
#define R57 0x39
#define R59 0x3B
#define R60 0x3C
#define R61 0x3D
#define R62 0x3E
#define R63 0x3F
#define R64 0x40
#define R65 0x41
#define R66 0x42
#define R67 0x43
#define R68 0x44
#define R69 0x45
#define R70 0x46
#define R71 0x47
#define R72 0x48
#define R73 0x49
#define R74 0x4A
#define R75 0x4B
#define R76 0x4C
#define R77 0x4D
#define R78 0x4E
#define R79 0x4F
#define R80 0x50
#define R81 0x51
#define R82 0x52
#define R83 0x53
#define R96 0x60
#define R97 0x61
#define R106 0x6A
#define R118 0x76
#define R128 0x80
#define R129 0x81
#define R130 0x82
#define R131 0x83
#define R132 0x84
#define R133 0x85
#define R134 0x86
#define R135 0x87
#define R136 0x88
#define R137 0x89
#define R139 0x8B
#define R140 0x8C
#define R141 0x8D
#define R143 0x8F
#define R144 0x90
#define R145 0x91
#define R146 0x92
#define R147 0x93
#define R148 0x94
#define R149 0x95
#define R150 0x96
#define R151 0x97
#define R152 0x98
#define R153 0x99
#define R154 0x9A
#define R157 0x9D
#define R192 0xC0
#define R193 0xC1
#define R229 0xE5
	
/*****************************************************************************
* @brief   api 
*****************************************************************************/
void lcd_hal_init(void);
void lcd_clear(u16 Color);

/*****************************************************************************
* @brief   base , used inner, cs manage inner
*****************************************************************************/
void lcd_cursor_set(u16 x, u16 y);
void lcd_window_set(u16 x1, u16 y1, u16 x2, u16 y2);

/*****************************************************************************
void lcd_write_gram_begin(void);
void lcd_write_gram(data);
void lcd_write_gram_end(void);
*****************************************************************************/

#endif
